﻿using IterationLife.ContentResources.Models;
using IterationLife.ContentResources.Queries;
using IterationLife.ContentResources.Repositories;
using IterationLife.Datas;
using IterationLife.PlatformBackEnd.AppServices.Contracts.ContentResources;
using IterationLife.PlatformBackEnd.AppServices.Dtos.ContentResources;
using Util.Applications;
using Util.Datas.Queries;
using Util.Domains.Trees;

namespace IterationLife.PlatformBackEnd.AppServices.Impl.ContentResources
{
    /// <summary>
    /// 内容资源评论服务
    /// </summary>
    public class ContentResourceCommentService : TreeServiceBase<ContentResourceComment, ContentResourceCommentDto, ContentResourceCommentQuery>, IContentResourceCommentService
    {
        /// <summary>
        /// 初始化内容资源评论服务
        /// </summary>
        /// <param name="unitOfWork">工作单元</param>
        /// <param name="contentResourceCommentRepository">内容资源评论仓储</param>
        public ContentResourceCommentService(IApplicationUnitOfWork unitOfWork, IContentResourceCommentRepository contentResourceCommentRepository)
            : base(unitOfWork, contentResourceCommentRepository)
        {
            ContentResourceCommentRepository = contentResourceCommentRepository;
        }

        /// <summary>
        /// 内容资源评论仓储
        /// </summary>
        protected IContentResourceCommentRepository ContentResourceCommentRepository { get; set; }

        /// <summary>
        /// 转换为数据传输对象
        /// </summary>
        /// <param name="entity">实体</param>
        protected override ContentResourceCommentDto ToDto(ContentResourceComment entity)
        {
            return entity.ToDto();
        }

        /// <summary>
        /// 转换为实体
        /// </summary>
        /// <param name="dto">数据传输对象</param>
        protected override ContentResourceComment ToEntity(ContentResourceCommentDto dto)
        {
            return dto.ToEntity();
        }

        /// <summary>
        /// 创建查询对象
        /// </summary>
        /// <param name="param">内容资源评论查询实体</param>
        protected override IQueryBase<ContentResourceComment> CreateQuery(ContentResourceCommentQuery param)
        {
            return new Query<ContentResourceComment>(param)
                .Filter(new TreeCriteria<ContentResourceComment>(param))
                .Filter(t => t.ContentResourceId == param.ContentResourceId)
                .Filter(t => t.UserName.Contains(param.UserName));
        }
    }
}